package arch.service.impl.usync;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import arch.dao.usync.SyncLogDao;
import arch.domain.common.PageObject;
import arch.domain.usync.SyncLog;
import arch.service.usync.SyncLogService;
import arch.util.UUID;
import arch.view.usync.SyncLogView;
/**
 * 
 * @author <a mailto="wanglong635@126.com">Long</a>
 *
 */
@Transactional
@Service("syncLogService")
public class SyncLogServiceBean implements SyncLogService {
	@Resource private SyncLogDao syncLogDao;
	private final Logger logger = Logger.getLogger(SyncLogServiceBean.class);
	
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<SyncLog> findList(SyncLogView searchCondition,
			PageObject pageObject) {
		StringBuffer sql = new StringBuffer();
		StringBuffer count = new StringBuffer();
		sql.append(" select ID, EXIST, TYPE, UNIQUE, CONTENT, CREATE_TIME from ( ")
			.append(" select rownumber() over() as num ,ID, EXIST, TYPE, UNIQUE, CONTENT, CREATE_TIME ")
			.append(" from SY_LOGGER ");
		count.append(" select count(*) from SY_LOGGER "); 
		
		
		StringBuffer condition = null;
		if(searchCondition!=null){
			boolean exist = searchCondition.isExist();
			String type = searchCondition.getType();
			String content = searchCondition.getContent();
			String beginTime = searchCondition.getBeginCreateTime();
			String endTime = searchCondition.getEndCreateTime();
			
			condition = new StringBuffer();
			condition.append(" where ");
			if(exist)
				condition.append(" EXIST = '1' and ");
			else
				condition.append(" EXIST = '0' and ");
			if(type!=null && !"".equals(type))
				condition.append(" TYPE = '").append(type).append("' and ");
			if(content!=null && !"".equals(content))
				condition.append(" CONTENT like '%").append(content).append("%' and ");
			if(beginTime!=null && !"".equals(beginTime))
				condition.append(" CREATE_TIME >= '").append(beginTime).append("' and ");
			if(endTime!=null && !"".equals(endTime))
				condition.append(" CREATE_TIME <= '").append(endTime).append("' and ");
			
			
		}
		
		if(condition!=null){
			String cond =condition.substring(0,condition.lastIndexOf("and"));
			sql.append(cond);
			count.append(cond);
		}
		sql.append(") where num > ? and num <= ? ");
		
		logger.debug("SQL > "+sql.toString());
		pageObject.setPageObject(this.syncLogDao.findCount(count.toString()), pageObject);
		
		return this.syncLogDao.findList(sql.toString(),pageObject.getStart(),pageObject.getEnd());
	}

	public void add(SyncLog entity) {
		List<SyncLog> logs = findByUnique(entity);
		if(logs==null||logs.size() == 0){
			String id = UUID.id("SY", "LG");
			entity.setId(id);
			entity.setCreateTime(new Timestamp(new Date().getTime()));

			this.syncLogDao.persist(entity);
		}
	}

	public void delete(SyncLog primaryKey) {
		this.syncLogDao.remove(primaryKey.getId());
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public SyncLog find(SyncLog primaryKey) {
		return this.syncLogDao.find(primaryKey.getId());
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	private List<SyncLog> findByUnique(SyncLog entity){
		StringBuffer sql = new StringBuffer();
		sql.append(" select ID, EXIST, TYPE, UNIQUE, CONTENT, CREATE_TIME from SY_LOGGER ")
			.append(" where UNIQUE = ? ");
		
		return this.syncLogDao.findList(sql.toString(), entity.getUnique());
	}
	
	public void update(SyncLog entity) {
		this.syncLogDao.merge(entity);
	}

	public void delete(List<SyncLog> logs) {
		this.syncLogDao.remove(logs);
	}

}
